package com.example.demo.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.example.demo.Common.CommonIT;
import com.example.demo.Excel.ExcelUtils;
import com.example.demo.annotation.AuthToken;
import com.example.demo.entity.Role;
import com.example.demo.service.IRoleService;
import com.xc.roadRisk.common.utils.ResponseUtils;
import com.xc.roadRisk.common.vo.PageList;
import com.xc.roadRisk.common.vo.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* <p>
*  前端控制器
* </p>
*
* @author hanfei
* @since 2020-04-08
*/
@Api(value = "角色",tags = "角色")
@RestController
public class RoleController {
   @Autowired
   private IRoleService roleApi;

   @ApiOperation("创建单个Role")
   @ApiImplicitParam(name = "version", paramType = "path")
   @PostMapping("pv/roles")
   @AuthToken
   public Response<Boolean> insert(@RequestBody Role role) {
       CommonIT commonIT=new CommonIT();
       String id=commonIT.findId();
       role.setId(id);
       Response<Boolean> response=ResponseUtils.returnObjectSuccess(roleApi.insert(role));
       response.setMsg(id);
       return response;
   }

   @ApiOperation("编辑单个Role")
   @ApiImplicitParam(name = "version", paramType = "path")
   @PutMapping("pv/roles/{uid}")
   @AuthToken
   public Response<Boolean> updateById(@PathVariable("uid") String uid, @RequestBody Role role) {
       role.setId(uid);
       return ResponseUtils.returnObjectSuccess(roleApi.updateById(role));
   }
   @ApiOperation("查询单个Role")
   @ApiImplicitParam(name = "version", paramType = "path")
   @GetMapping("pb/roles/{uid}")
   @AuthToken
   public Response<Role> selectById(@PathVariable("uid") String uid) {
       return ResponseUtils.returnObjectSuccess(roleApi.selectById(uid));
   }

   @ApiOperation("查询分页Role")
   @ApiImplicitParam(name = "version", paramType = "path")
   @GetMapping("pb/roles/action/search")
   @AuthToken
   public Response<PageList<Role>> selectPage(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                              Role role) {
       Page<Role> page = roleApi.selectPage(new Page<>(pageNo, pageSize), new EntityWrapper<>(role));
       return ResponseUtils.returnObjectSuccess(new PageList<>(page));
   }
   @ApiOperation("模糊查询分页")
   @ApiImplicitParam(name = "version", paramType = "path")
   @GetMapping("pb/roles/action/findByKey")
   @AuthToken
   public Response<PageList<Role>> findByKey(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                             @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                             @RequestParam(value = "key", required = false) String key) {
       Page<Role> page = roleApi.findByKey(new Page<>(pageNo, pageSize), key);
       return ResponseUtils.returnObjectSuccess(new PageList<>(page));
       }

   @ApiOperation("查询所有")
   @ApiImplicitParam(name = "version", paramType = "path")
   @GetMapping("pb/roles/findall")
   @AuthToken
   public Response<List<Role>> findall(Role role) {
       return ResponseUtils.returnObjectSuccess(roleApi.selectList(new EntityWrapper<>(role)));
       }

   @ApiOperation("导出")
   @GetMapping("/roles/export")
   @AuthToken
   public String exportAll(HttpServletResponse response,Role role) {
       List<Role> list=roleApi.selectList(new EntityWrapper<>(role));
       ExcelUtils.writeExcel(response,list,Role.class);
       return null;
       }
   @ApiOperation("导入")
   @PostMapping("/roles/import")
   @AuthToken
   public Response importAll(@RequestParam("file")MultipartFile file){
       List<Role> list=ExcelUtils.readExcel(null,Role.class,file);
       for(Role a:list){
       CommonIT commonIT=new CommonIT();
       String id=commonIT.findId();
       a.setId(id);
       }

       return ResponseUtils.returnObjectSuccess(roleApi.insertBatch(list));
       }
@ApiOperation("模糊查询加定向查询分页")
@ApiImplicitParam(name = "version", paramType = "path")
@GetMapping("pb/roles/action/findByKeyAndAll")
@AuthToken
public Response<PageList<Role>> findByKeyAndAll(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                @RequestParam(value = "key",required = false) String key,
                                                Role entity) {
       Page<Role> page = roleApi.findByKeyAndAll(new Page<>(pageNo, pageSize), key,entity);
       return ResponseUtils.returnObjectSuccess(new PageList<>(page));
       }
}
